import { PrismaService } from '@app/prisma';
import { RandomNameService } from '@app/utils/random-name/random-name.service';
import { Injectable } from '@nestjs/common';
import { TaskService } from 'src/task/task.service';

@Injectable()
export class TestService {
    constructor(
        private readonly prisma: PrismaService,
        private readonly taskService: TaskService,
        private readonly randomNameService: RandomNameService,
    ) { }
    async allin(taskId: number) {
        const task = await this.prisma.task.findUnique({
            where: {
                id: taskId
            },
            include: {
                UserOnTask: true,
                TaskItem: true,
                TaskOnClasses: {
                    include: {
                        classes: {
                            include: {
                                User: true,
                                TeacherOnClasses: {
                                    include: {
                                        teacher: true
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });
        const items = task.TaskItem;
        const set = new Set();
        for (const userTask of task.UserOnTask) {
            set.add(`${userTask.userId}_${userTask.teacherId}`);
        }
        for (const { classes } of task.TaskOnClasses) {
            for (const user of classes.User) {
                for (const { teacherId } of classes.TeacherOnClasses) {
                    if (set.has(`${user.id}_${teacherId}`)) continue;
                    const form = items.reduce((acc, item) => {
                        acc[item.id] = Math.floor(Math.random() * 4 + 1)
                        return acc;
                    }, {} as {
                        [k in number]: number
                    })
                    await this.taskService.createTaskOnUser(
                        user.id,
                        teacherId,
                        task.id,
                        Math.random() > 0.3 ? this.randomNameService.getNickName() : "",
                        items,
                        form
                    ).catch(e => {
                        console.log("err")
                        console.log(user.id, teacherId)
                        console.log("-----------")
                    })
                    for (const item of items) {
                        await this.prisma.taskOnTeacherItem.update({
                            where: {
                                taskItemId_teacherId: {
                                    teacherId: teacherId,
                                    taskItemId: item.id
                                }
                            },
                            data: {
                                score: {
                                    increment: form[item.id],
                                },
                                count: {
                                    increment: 1
                                }
                            }
                        })
                    }

                }

            }
        }
    }
}
